function init() {
    document.getElementById("date_select").value = 3;
    hideDateElements();
}

function refreshElements() {
    document.getElementById("message_box").value = "";
}

function disableElements() {    
    document.getElementById("date_select").disabled = true;
    document.getElementById("date_part").disabled = true;
    document.getElementById("time_part").disabled = true;
    document.getElementById("message_box").disabled = true;
    document.getElementById("submit_button").disabled = true;
}

function enableElements() {
    document.getElementById("date_select").disabled = false;
    document.getElementById("date_part").disabled = false;
    document.getElementById("time_part").disabled = false;
    document.getElementById("message_box").disabled = false;
    document.getElementById("submit_button").disabled = false;
}

function hideDateElements() {
    document.getElementById("date_part").style.display = "none";
    document.getElementById("time_part").style.display = "none";
}

function showDatePart() {
    document.getElementById("date_part").style.display = "";
}

function showTimePart() {
    document.getElementById("time_part").style.display = "";
}

function dateSelectOnChange(value) {
    hideDateElements();

    if (value <= 2) {
        showTimePart();
    }
    else if (value >= 4 && value <= 5) {
        showTimePart();
    }
    else if (value == 6) {
        showDatePart();
        showTimePart();
    }
}

xhr = function () {
    var a = null;

    if (window.XMLHttpRequest) {
        try  {
            a = new XMLHttpRequest();
        }
        catch (ex) {
        ;
        }
    }
    else if (window.ActiveXObject) {
        for (var i = 0, arr;arr = ["MSXML2.XMLHTTP.6.0", "MSXML2.XMLHTTP.3.0", "MSXML2.XMLHTTP", "Microsoft.XMLHTTP"][i++];) {
            try  {
                a = new ActiveXObject(arr);
                break 
            }
            catch (ex) {
                ;
            }
        }
    }
    return a;
};

var xr;
function doSubmitMessage() {    
    xr = xhr();    
    var m = encodeURIComponent(document.getElementById("message_box").value);    
    var ds = encodeURIComponent(document.getElementById("date_select").value);    
    var dm = encodeURIComponent(document.getElementById("date_month").value);    
    var dd= encodeURIComponent(document.getElementById("date_day").value);    
    var th= encodeURIComponent(document.getElementById("time_hour").value);    
    var tm= encodeURIComponent(document.getElementById("time_minute").value);    
    
    var url = "/pub/Submit";
    var params = "ds=" + ds + "&dm=" + dm + "&dd=" + dd + "&th=" + th+ "&tm=" + tm + "&m=" + m;    
    disableElements();
    
    if (xr) {
        xr.onreadystatechange = processAjaxCall;                
        xr.open("POST", url, true);                                
        xr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
        xr.setRequestHeader("Content-length", params.length);
        xr.setRequestHeader("Connection", "close");
        xr.send(params);
    }
    else  {
        enableElements();
    }
}

function processAjaxCall() {
    if (xr.readyState == 4) {            
        if (xr.status == 200) {
            if (xr.responseText != "" || xr.responseText != null)                
                processRecentPosts(xr.responseText);            
        }
        enableElements();
        refreshElements();
    }
}

function processRecentPosts(data) {    
    var eventData = JSON.parse(data);
    var content = document.getElementById("recent_posts");
    
    var table = '<table width="100%" cellpadding="5" cellspacing="0" boder="0" class="rp">';

    var i = 0;
    for (i in eventData.events) {    
        table +=  '<tr>' +
                    '<td width="50px">' +
                        '<img src="/style/images/man.png"' + 
                    '</td>' + 
                    '<td>' +
                        '<h3>' + eventData.events[i].owner.owner_fullname + 
                            '<em>' + eventData.events[i].event_date + '</em>' + 
                        '</h3>' +
                        '<span class="message">' +
                            eventData.events[i].event_message +
                        '</span>' +
                        '<h4>';                        
                            if (eventData.events[i].event_photocount != 0) {
                                table += '<a href="javascript: void(0);" ' +
                                         'onclick="alert(1); ' +
                                         'style="background-image: url(/style/images/h/photo-icon.png);" ' +
                                         'class="footerlink">' + eventData.events[i].event_photocount + '</a>';
                            }
                            if (eventData.events[i].event_videocount != 0) {
                                table += '<a href="javascript: void(0);" ' +
                                         'onclick="alert(1); ' +
                                         'style="background-image: url(/style/images/h/video-icon.png);" ' +
                                         'class="footerlink">' + eventData.events[i].event_videocount + '</a>';
                            }                                                    
                        table += '</h4>' +
                    '</td>' +
                  '</tr>';
    }
    table += '</table>';
    
    content.innerHTML = table;
}
       
function closeAppWindow() {
    document.getElementById("modalPage").style.display = "none";
}

function doShowEApp(id) {    
    document.getElementById("modalPage").style.display = "block";
        
    var mb = document.getElementById("modalBody");
    var mbInner = '<iframe id="appFrame" name="appFrame" frameborder="0" scrolling="no" width="600px" height="400px" src="/test/MapsTest.html"/>'                            
    mb.innerHTML = mbInner;
    /*
    if (!xr) {
        xr = xhr();
    }
    
    appUrl = "/apps/Maps";
    if (xr) {
        xr.onreadystatechange = retrieveApp;
        xr.open("GET", appUrl, true);
        xr.send(null);
    }
    */
}


function retrieveApp() {
    if (xr.readyState == 4) {            
        if (xr.status == 200) {            
            document.getElementById("modalBody").innerHTML = xr.responseText;                        
        }        
    }
}